package com.flying.client.utils;

import android.util.Log;

public class Logger {
	public static final int LOGLEVEL_DEBUG = 1;
	public static final int LOGLEVEL_WRAN = 2;
	public static final int LOGLEVEL_INFO = 3;
	public static final int LOGLEVEL_ERROR = 4;
	
	private static String curLogTag = "ouyu";
	private static int curLogLevel = LOGLEVEL_DEBUG;
	private static boolean isShowThreadInfo = true;
	
	public static void setLogLevel(int level)
	{
		curLogLevel = level;
	}
	
	public static void setLogTag(String tag)
	{
		curLogTag = tag;
	}
	
	public static void setShowThread(boolean flag)
	{
		isShowThreadInfo = flag;
	}
	
	
	public static void init(int level,String tag,boolean isShowThread)
	{
		setLogLevel(level);
		setLogTag(tag);
		setShowThread(isShowThread);
	}
	
	public static Logger getLogger(String className)
	{
		return new Logger(className);
	}
	
	private String className;
	
	private Logger(String className)
	{
		this.className = className;
	}
	
	public void debug(String msg)
	{
		write(Log.DEBUG, msg);
	}
	
	public void warn(String msg)
	{
		write(Log.WARN, msg);
	}
	
	public void error(String msg)
	{
		write(Log.ERROR, msg);
	}
	
	public void info(String msg)
	{
		write(Log.INFO,msg);
	}
	
	private void write(int priority,String msg)
	{
		if (curLogLevel > priority)
			return;
		
		StringBuilder sb = new StringBuilder(msg);
		
		sb.insert(0, String.format("[%s]",className));
		
		if (isShowThreadInfo)
		{
			sb.insert(0, String.format("[%s:%s]",Thread.currentThread().getId(),
					Thread.currentThread().getName()));
		}
		
		Log.println(priority, curLogTag, sb.toString());
	}
}
